package com.hsq.upload;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPFileUploadServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务在8888端口监听......");
        //等待连接
        Socket socket = serverSocket.accept();
        //读取客户端发送的数据，通过Socket得到输入流
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        String destFilePath = "src\\J2.jpg";
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFilePath));
        //写入
        bos.write(bytes);

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(outputStream)
        );
        bufferedWriter.write("收到图片");
        bufferedWriter.flush(); //注意！！！！！！！！！把内容刷新到数据通道
        socket.shutdownOutput();//设置结束标志
//        bufferedWriter.newLine();
        //关闭
        bufferedWriter.close();
        bos.close();
        bis.close();
        socket.close();
        serverSocket.close();
    }
}
